package com.otaku.member.SysException.handler;

import com.otaku.common.ResultUtils.ResultModel;
import com.otaku.common.ResultUtils.ResultCode;
import com.otaku.common.ResultUtils.ResultUtil;
import com.otaku.member.SysException.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandlers {

    private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandlers.class);


    /**
     * 特定异常接收类
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = MyException.class)
    public ResultModel handel(Exception e) {
        ResultModel result;
        if (e instanceof MyException) {
            result = ResultUtil.error(((MyException) e).getResultCode());
            LOGGER.info("异常处理返回值{}", result.toString());
            return result;
        } else {
            return ResultUtil.error(ResultCode.PARAMS_ERROR);
        }
    }

    /**
     * 全部异常接收类
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResultModel allExceptionHandel(Exception e) {
        LOGGER.error("系统异常捕获：{}", e);
        return ResultUtil.error(ResultCode.SERVER_ERROR);
    }


}
